<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ThongKeController
 *
 * @author Vu
 */
include APPLICATION_PATH."/models/Product.php";
include APPLICATION_PATH."/models/Order.php";
include APPLICATION_PATH."/models/Statistic.php";
class Admin_StatisticController extends Zend_Controller_Action {
    //put your code here
   public function init(){
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->modelMapper = new Model_StatisticMapper();
    }
    public function statisticproductbuy1Action(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $product_id=$this->_getParam("product_id","");
        $groupproduct_id=$this->_getParam("groupproduct_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductby1($fromdate,$todate, $product_id, $groupproduct_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);        
        exit();
    }
    public function statisticproductbuyAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $product_id=$this->_getParam("product_id","");
        $groupproduct_id=$this->_getParam("groupproduct_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductby($fromdate,$todate, $product_id, $groupproduct_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);        
        exit();
    }
    
    public function indexAction(){        
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->product_id = "";
        $this->view->groupproduct_id = "";
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            if (isset($_POST['product_id'])){
                $product_id = array();
                foreach ($_POST['product_id'] as $key => $value) {
                    array_push($product_id,$value);                
                }            
                $this->view->product_id = implode(",", $product_id);
            }
            if (isset($_POST['groupproduct_id'])){
                $groupproduct_id = array();
                foreach ($_POST['groupproduct_id'] as $key => $value) {
                    array_push($groupproduct_id,$value);                
                }            
                $this->view->groupproduct_id = implode(",", $groupproduct_id);
            }
       }
       $this->view->productHTML=  GlobalLib::getComboMulti("product_id", "product", "id", "name",NULL,$this->view->product_id,true);
       $this->view->groupproductHTML=  GlobalLib::getComboMulti("groupproduct_id", "product_category", "id", "name",NULL,$this->view->groupproduct_id,true);
    
    }
    public function index1Action(){        
        $this->view->fromdate="";
        $this->view->todate="";
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            if (isset($_POST['product_id'])){
                $product_id = array();
                foreach ($_POST['product_id'] as $key => $value) {
                    array_push($product_id,$value);                
                }            
                $this->view->product_id = implode(",", $product_id);
            }
            if (isset($_POST['groupproduct_id'])){
                $groupproduct_id = array();
                foreach ($_POST['groupproduct_id'] as $key => $value) {
                    array_push($groupproduct_id,$value);                
                }            
                $this->view->groupproduct_id = implode(",", $groupproduct_id);
            }
       }
       $this->view->productHTML=  GlobalLib::getComboMulti("product_id", "product", "id", "name",NULL,$this->view->product_id,true);
       $this->view->groupproductHTML=  GlobalLib::getComboMulti("groupproduct_id", "product_category", "id", "name",NULL,$this->view->groupproduct_id,true);
    
    }
    public function provinceAction(){
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->area_id = 0;
        $this->view->showchart=1;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            $this->view->showchart = $_POST["showchart"];
       }
       $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','');
        
    }
    public function statisticproductprovincebuyAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductprovincebuyby($fromdate,$todate, $area_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticproductprovincesaleAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductprovincesaleby($fromdate,$todate, $area_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    
    //area
    public function areaAction(){
        $this->view->fromdate="";
        $this->view->todate=""; 
        $this->view->showchart=1;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->showchart = $_POST["showchart"]; 
            
       }
    }
    public function statisticproductareabuyAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");        
        $menber   = array();
        foreach ($this->modelMapper->statisticproductareabuyby($fromdate,$todate) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticproductareasaleAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");        
        $menber   = array();
        foreach ($this->modelMapper->statisticproductareasaleby($fromdate,$todate) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    //district
    public function districtAction(){
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->area_id=0;
        $this->view->province_id=0;
        $this->view->showchart=1;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            $this->view->province_id = $_POST["province_id"];
            $this->view->showchart = $_POST["showchart"]; 
       }
        $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','onchange="getProvince(\'' . $this->aConfig["site"]["url"] . 'admin/service/index\',$(\'#area_id\').val(),\'province_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->view->province_id , false, 'form-control', '', 'where id in (select province_id from area_province_category where area_id= '.$this->view->area_id.' )', '');
        
    }
    public function statisticproductdistrictbuyAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $province_id=$this->_getParam("province_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductdistrictbuyby($fromdate,$todate,$area_id,$province_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticproductdistrictsaleAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $province_id=$this->_getParam("province_id","");
        $menber   = array();
        foreach ($this->modelMapper->statisticproductdistrictsaleby($fromdate,$todate, $area_id, $province_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    
    //agent
    public function agentAction(){
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->agent_id = "0";
        $this->view->product_id = "0";
        $this->view->province_id = "0";

        $this->view->showchart=1;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->province_id = $_POST["province_id"];
            if (isset($_POST['agent_id'])){
                $agent_id = array();
                foreach ($_POST['agent_id'] as $key => $value) {
                    array_push($agent_id,$value);                
                }
                $this->view->agent_id = implode(",", $agent_id);
            }
            if (isset($_POST['product_id'])){
                $product_id = array();
                foreach ($_POST['product_id'] as $key => $value) {
                    array_push($product_id,$value);                
                }            
                $this->view->product_id = implode(",", $product_id);
            }
            $this->view->showchart = $_POST["showchart"]; 
            
       }
        $filter = $this->view->agent_id!='0'?" where agent.province_id =".$_POST["province_id"]:"";
    $this->view->agentHTML=  GlobalLib::getComboMulti("agent_id", "agent", "id", "name","address",$this->view->agent_id,true,$filter);
    $this->view->productHTML=  GlobalLib::getComboMulti("product_id", "product", "id", "name",NULL,$this->view->product_id,true);
    $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->view->province_id ,false, 'form-control','', '','','');
    
    }
    public function statisticagentcolumnAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $agent_id=$this->_getParam("agent_id","");
        $product_id=$this->_getParam("product_id","");

        $menber = array();
        foreach ($this->modelMapper->statisticagentcolumnby($fromdate,$todate, $agent_id, $product_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticagentpercentbuyAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $agent_id=$this->_getParam("agent_id","");
        $product_id=$this->_getParam("product_id","");

        $menber = array();
        foreach ($this->modelMapper->statisticagentpercentbuyby($fromdate,$todate, $agent_id, $product_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticagentpercentsaleAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $agent_id=$this->_getParam("agent_id","");
        $product_id=$this->_getParam("product_id","");

        $menber = array();
        foreach ($this->modelMapper->statisticagentpercentsaleby($fromdate,$todate, $agent_id, $product_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    public function statisticagentgridAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $agent_id=$this->_getParam("agent_id","");
        $product_id=$this->_getParam("product_id","");

        $menber = array();
        foreach ($this->modelMapper->statisticagentgridby($fromdate,$todate, $agent_id, $product_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
     
   }
    
    //truyen
    //thong ke province
     public function testAction(){
        $this->view->fromdate="";
        $this->view->todate=""; 
        $this->view->area_id="";
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            
       }
       $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','');
//       $menber = array();
//        foreach ($this->modelMapper->test($_POST["from_date"],$_POST["to_date"],$_POST["area_id"]) as $items) {
//            //$entry = new Model_Province();
//            array_push($menber, $items);
//                     //$entries[] = $entry;
//        }
//        Zend_Debug::dump(json_encode($menber));
//        exit();
    }
    public function gettestAction(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $menber = array();
        foreach ($this->modelMapper->test($fromdate,$todate,$area_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    //area
    public function test1Action()
    {
        $this->view->fromdate="";
        $this->view->todate=""; 
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
                     
       }
    }
    public function gettest1Action(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        
        $menber = array();
        foreach ($this->modelMapper->test1($fromdate,$todate) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        //var_dump($menber[1]);
        echo json_encode($menber);         
        exit();
    }
   //district
   public function test2Action(){
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->area_id=0;
        $this->view->province_id=0;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            $this->view->province_id = $_POST["province_id"];
       }
        $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','onchange="getProvince(\'' . $this->aConfig["site"]["url"] . 'admin/service/index\',$(\'#area_id\').val(),\'province_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->view->province_id , false, 'form-control', '', 'where id in (select province_id from area_province_category where area_id= '.$this->view->area_id.' )', '');
        
   }
   public function gettest2Action(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $province_id =$this->_getParam("province_id","");
        $menber = array();
        foreach ($this->modelMapper->test2($fromdate,$todate,$area_id,$province_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    //thong ke theo kieu datagrid
    //khu vuc
    public function test4Action(){
        $this->view->fromdate="";
        $this->view->todate="";

        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
           
       }
     
   }
   public function gettest4Action(){
        //$this->view->controller = $this; 
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");

        $menber = array();
        foreach ($this->modelMapper->test4($fromdate,$todate) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);
        //Zend_Debug::dump($menber);
        
        exit();
    }
    //district dang bang
   public function test5Action(){
        $this->view->fromdate="";
        $this->view->todate="";
        $this->view->area_id=0;
        $this->view->province_id=0;
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            $this->view->province_id = $_POST["province_id"];
       }
        $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','onchange="getProvince(\'' . $this->aConfig["site"]["url"] . 'admin/service/index\',$(\'#area_id\').val(),\'province_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->view->province_id , false, 'form-control', '', 'where id in (select province_id from area_province_category where area_id= '.$this->view->area_id.' )', '');
        
   }
   public function gettest5Action(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $province_id =$this->_getParam("province_id","");
        $menber = array();
        foreach ($this->modelMapper->test5($fromdate,$todate,$area_id,$province_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
    //thong ke province theo dang bang
     public function test6Action(){
        $this->view->fromdate="";
        $this->view->todate=""; 
        $this->view->area_id="";
        if($this->getRequest()->isPost()){
            $this->view->fromdate = $_POST["from_date"];
            $this->view->todate = $_POST["to_date"];
            $this->view->area_id = $_POST["area_id"];
            
       }
       $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->view->area_id ,false, 'form-control','', '','','');

    }
    public function gettest6Action(){
        $this->_helper->layout()->disableLayout();
        $fromdate=$this->_getParam("fromdate","");
        $todate=$this->_getParam("todate","");
        $area_id =$this->_getParam("area_id","");
        $menber = array();
        foreach ($this->modelMapper->test6($fromdate,$todate,$area_id) as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
                     //$entries[] = $entry;
        }
        echo json_encode($menber);         
        exit();
    }
}
